using AutoMapper;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using B.S.RbacData.ErrorCode;
using SupplierCustomerManagement.Write.API.Applications.Commands.suppliercontacts;

namespace SupplierCustomerManagement.Write.API.Controllers
{
    /// <summary>
    /// 供应商联系人管理控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class SupplierContactsController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly IMapper _mapper;
        private readonly ILogger<SupplierContactsController> _logger;

        public SupplierContactsController(
            IMediator mediator,
            IMapper mapper,
            ILogger<SupplierContactsController> logger)
        {
            _mediator = mediator;
            _mapper = mapper;
            _logger = logger;
        }

        /// <summary>
        /// 添加供应商联系人
        /// </summary>
        /// <param name="command">供应商联系人信息</param>
        /// <returns>添加结果</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddSupplierContact([FromBody] AddSupplierContactCommand command)
        {
            try
            {
                _logger.LogInformation("开始添加供应商联系人，供应商ID: {SupplierId}, 联系人: {ContactPerson}", 
                    command.SupplierId, command.ContactPerson);

                // 发送命令
                var result = await _mediator.Send(command);

                if (result.IsSuccess)
                {
                    _logger.LogInformation("成功添加供应商联系人，ID: {ContactId}, 供应商ID: {SupplierId}, 联系人: {ContactPerson}", 
                        result.data, command.SupplierId, command.ContactPerson);
                }
                else
                {
                    _logger.LogWarning("添加供应商联系人失败: {Message}, 供应商ID: {SupplierId}, 联系人: {ContactPerson}", 
                        result.message, command.SupplierId, command.ContactPerson);
                }

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "添加供应商联系人时发生异常，供应商ID: {SupplierId}, 联系人: {ContactPerson}", 
                    command.SupplierId, command.ContactPerson);
                return ApiResult<int>.Error($"添加供应商联系人时发生异常: {ex.Message}");
            }
        }

        /// <summary>
        /// 修改供应商联系人
        /// </summary>
        /// <param name="id">联系人ID</param>
        /// <param name="command">供应商联系人修改信息</param>
        /// <returns>修改结果</returns>
        [HttpPost]
        public async Task<ApiResult<bool>> UpdateSupplierContact([FromBody] UpdateSupplierContactCommand command)
        {
            try
            {
               
                _logger.LogInformation("开始修改供应商联系人，ID: {ContactId}, 供应商ID: {SupplierId}, 联系人: {ContactPerson}", 
                    command.Id, command.SupplierId, command.ContactPerson);

                // 发送命令
                var result = await _mediator.Send(command);

                if (result.IsSuccess)
                {
                    _logger.LogInformation("成功修改供应商联系人，ID: {ContactId}, 供应商ID: {SupplierId}, 联系人: {ContactPerson}", 
                        command.Id, command.SupplierId, command.ContactPerson);
                }
                else
                {
                    _logger.LogWarning("修改供应商联系人失败: {Message}, ID: {ContactId}, 联系人: {ContactPerson}", 
                        result.message, command.Id, command.ContactPerson);
                }

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "修改供应商联系人时发生异常，ID: {ContactId}, 联系人: {ContactPerson}", 
                    command.Id, command.ContactPerson);
                return ApiResult<bool>.Error($"修改供应商联系人时发生异常: {ex.Message}");
            }
        }

        /// <summary>
        /// 删除供应商联系人
        /// </summary>
        /// <param name="id">联系人ID</param>
        /// <param name="command">删除信息</param>
        /// <returns>删除结果</returns>
        [HttpPost]
        public async Task<ApiResult<bool>> DeleteSupplierContact([FromBody] DeleteSupplierContactCommand command)
        {
            try
            {

                _logger.LogInformation("开始删除供应商联系人，ID: {ContactId}", command.Id);

                // 发送命令
                var result = await _mediator.Send(command);

                if (result.IsSuccess)
                {
                    _logger.LogInformation("成功删除供应商联系人，ID: {ContactId}", command.Id);
                }
                else
                {
                    _logger.LogWarning("删除供应商联系人失败: {Message}, ID: {ContactId}", 
                        result.message, command.Id);
                }

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "删除供应商联系人时发生异常，ID: {ContactId}", command.Id);
                return ApiResult<bool>.Error($"删除供应商联系人时发生异常: {ex.Message}");
            }
        }
    }
} 